﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace myMoney.Finance
{
    public partial class ucChart : UserControl
    {
        public ucChart()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.dtpStart.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01"));
            loadData();
            btnCheck_Click(this.btnCheck, e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.tvClassify.BackColor = Main.AeroColor;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            DataTable dt0, dt1;
            if (this.tvClassify.SelectedNode == null || this.tvClassify.SelectedNode.Name.ToUpper() == "ROOT")
            {
                dt0 = MoneyClass.Expenses.get(Main.CurrentGuid, "0", null, this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.AddDays(1).ToString("yyyy-MM-dd"), this.cbbType.SelectedValue.ToString());
                dt1 = MoneyClass.Expenses.get(Main.CurrentGuid, "1", null, this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.AddDays(1).ToString("yyyy-MM-dd"), this.cbbType.SelectedValue.ToString());
            }
            else if (this.tvClassify.SelectedNode.Name.ToUpper() == "INCOME")
            {
                dt0 = MoneyClass.Expenses.get(Main.CurrentGuid, "0", null, this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.AddDays(1).ToString("yyyy-MM-dd"), this.cbbType.SelectedValue.ToString());
                dt1 = new DataTable();
            }
            else if (this.tvClassify.SelectedNode.Name.ToUpper() == "EXPENSES")
            {
                dt0 = new DataTable();
                dt1 = MoneyClass.Expenses.get(Main.CurrentGuid, "1", null, this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.AddDays(1).ToString("yyyy-MM-dd"), this.cbbType.SelectedValue.ToString());
            }
            else
            {
                dt0 = MoneyClass.Expenses.get(Main.CurrentGuid, "0", this.tvClassify.SelectedNode.Name, this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.AddDays(1).ToString("yyyy-MM-dd"), this.cbbType.SelectedValue.ToString());
                dt1 = MoneyClass.Expenses.get(Main.CurrentGuid, "1", this.tvClassify.SelectedNode.Name, this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.AddDays(1).ToString("yyyy-MM-dd"), this.cbbType.SelectedValue.ToString());
            }

            zedGraphPic.GraphPane.CurveList.Clear();
            zedGraphPic.GraphPane.GraphObjList.Clear();
            ZedGraph.PointPairList List0 = new ZedGraph.PointPairList();
            ZedGraph.PointPairList List1 = new ZedGraph.PointPairList();
            List0.Clear();
            List1.Clear();
            int i = 0, j = 0;
            List<string> myTime = new List<string>();
            List<double> val1 = new List<double>();
            List<double> val2 = new List<double>();
            DateTime myDate = new DateTime();
            string title = (this.tvClassify.SelectedNode == null ? this.tvClassify.Nodes[0].Text : this.tvClassify.SelectedNode.Text) + " {0}至{1} 走势图";
            switch (this.cbbType.SelectedValue.ToString().ToLower())
            {
                case "y":
                    myDate = DateTime.Parse(this.dtpStart.Value.AddYears(-1).ToString("yyyy-01-01"));
                    title = string.Format(title, this.dtpStart.Value.ToString("yyyy"), this.dtpEnd.Value.ToString("yyyy"));
                    break;
                case "m":
                    myDate = DateTime.Parse(this.dtpStart.Value.AddMonths(-1).ToString("yyyy-MM-01"));
                    title = string.Format(title, this.dtpStart.Value.ToString("yyyy-MM"), this.dtpEnd.Value.ToString("yyyy-MM"));
                    break;
                default:
                    myDate = this.dtpStart.Value;
                    title = string.Format(title, this.dtpStart.Value.AddDays(-1).ToString("yyyy-MM-dd"), this.dtpEnd.Value.ToString("yyyy-MM-dd"));
                    break;
            }
            while (true)
            {
                if (i < dt0.Rows.Count && myDate.Date == DateTime.Parse(dt0.Rows[i][0].ToString()))
                {
                    val1.Add(double.Parse(dt0.Rows[i][1].ToString()));
                    i++;
                }
                else
                {
                    val1.Add(0);
                }
                if (j < dt1.Rows.Count && myDate.Date == DateTime.Parse(dt1.Rows[j][0].ToString()))
                {
                    val2.Add(double.Parse(dt1.Rows[j][1].ToString()));
                    j++;
                }
                else
                {
                    val2.Add(0);
                }

                switch (this.cbbType.SelectedValue.ToString().ToLower())
                {
                    case "y":
                        myTime.Add(myDate.ToString("yyyy"));
                        myDate = myDate.AddYears(1);
                        break;
                    case "m":
                        myTime.Add(myDate.ToString("yyyy.MM"));
                        myDate = myDate.AddMonths(1);
                        break;
                    default:
                        myTime.Add(myDate.ToString("yy.MM.dd"));
                        myDate = myDate.AddDays(1);
                        break;
                }

                if (myDate > this.dtpEnd.Value) break;
            }

            this.zedGraphPic.GraphPane.Fill = new ZedGraph.Fill(Main.AeroColor, Main.AeroColor, 45.0F);
            this.zedGraphPic.GraphPane.Border = new ZedGraph.Border(false, Color.Black, 0);
            ZedGraph.LineItem myCurve;
            this.zedGraphPic.GraphPane.Title.Text = title;
            this.zedGraphPic.GraphPane.XAxis.Title.Text = "日期";
            this.zedGraphPic.GraphPane.YAxis.Title.Text = "金额";
            this.zedGraphPic.GraphPane.YAxis.Scale.Min = 0;
            this.zedGraphPic.GraphPane.XAxis.Type = ZedGraph.AxisType.Text;
            this.zedGraphPic.GraphPane.XAxis.Scale.TextLabels = myTime.ToArray();
            myCurve = zedGraphPic.GraphPane.AddCurve("收入", null, val1.ToArray(), Color.DarkGreen, ZedGraph.SymbolType.None);
            myCurve = zedGraphPic.GraphPane.AddCurve("支出", null, val2.ToArray(), Color.DarkRed, ZedGraph.SymbolType.None);
            this.zedGraphPic.IsShowPointValues = true;
            this.zedGraphPic.AxisChange();
            this.zedGraphPic.Refresh();
        }

        private void loadData()
        {
            #region 报告类型

            System.Collections.ArrayList Type = new System.Collections.ArrayList();
            Type.Add(new System.Collections.DictionaryEntry("d", "按天统计"));
            Type.Add(new System.Collections.DictionaryEntry("m", "按月统计"));
            Type.Add(new System.Collections.DictionaryEntry("y", "按年统计")); 
            this.cbbType.DisplayMember = "Value";
            this.cbbType.ValueMember = "Key";
            this.cbbType.DataSource = Type;
            this.cbbType.SelectedIndex = 0;

            #endregion

            #region 左侧树形菜单

            DataTable dt = null;
            TreeNode root = tvClassify.Nodes.Add("root", "收支总计");
            root.NodeFont = new Font("微软雅黑", 10, FontStyle.Bold);
            root.Expand();
            TreeNode Income = root.Nodes.Add("Income", "收入");
            Income.NodeFont = new Font("微软雅黑", 9, FontStyle.Bold);
            Income.Expand();
            dt = MoneyClass.Classify.getIncome(Main.CurrentGuid);
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode myNode = Income.Nodes.Add(dr["guid"].ToString(), dr["Classify"].ToString());
            }
            TreeNode Expenses = root.Nodes.Add("Expenses", "支出");
            Expenses.NodeFont = new Font("微软雅黑", 9, FontStyle.Bold);
            dt = MoneyClass.Classify.getExpense(Main.CurrentGuid);
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode myNode = Expenses.Nodes.Add(dr["guid"].ToString(), dr["Classify"].ToString());
            }
            this.tvClassify.ExpandAll();

            #endregion
        }

        private void tvClassify_AfterSelect(object sender, TreeViewEventArgs e)
        {
            btnCheck_Click(this.btnCheck, new EventArgs());
        }
    }
}
